package com.yw.android.letsgo.fragment.market;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.yw.android.common.base.BaseFragment;
import com.yw.android.letsgo.R;
import com.yw.android.letsgo.adapt.SellAdapt;
import com.yw.android.letsgo.bean.req.QueryDog;
import com.yw.android.letsgo.bean.res.PetsOnSaleItem;
import com.yw.android.letsgo.bean.res.SellDogInfo;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * ***********************************************************************
 * 项目名称： Yw_BaiduPet
 * 类描述：
 * 包名： com.yw.android.letsgo.fragment.market
 * 创建人：shanx
 * 创建时间： 2018/5/17 11:05
 * 修改人：shanx
 * 修改时间： 2018/5/17 11:05
 * 修改备注：
 * ***********************************************************************
 *
 * @version 1.0.0
 */
public class MarketFragment extends BaseFragment<MarketPresenter, MarketModel> implements MarketContract.View {
    private RecyclerView mSellRecyclerView;
    private SwipeRefreshLayout mSellSwipeRefreshLayout;
    private SellAdapt mSellAdapt;
    private List<PetsOnSaleItem> mPetsOnSaleItemList = new CopyOnWriteArrayList<>();
    private QueryDog mQueryDog;
    private Set<PetsOnSaleItem> mPetsOnSaleItemSet = new HashSet();

    /**
     * 初始化
     *
     * @param mBundle
     * @return
     */
    public static MarketFragment newInstance(Bundle mBundle) {
        MarketFragment fragment = new MarketFragment();
        fragment.setArguments(mBundle);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.frag_market;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(MarketFragment.this, mModel);
    }

    @Override
    public void initView() {
        mSellRecyclerView = rootView.findViewById(R.id.rv_sell);
        mSellRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mSellAdapt = new SellAdapt(this, mPetsOnSaleItemList, mSellRecyclerView);
        mSellAdapt.setOnMoreDataLoadListener(new SellAdapt.LoadMoreDataListener() {
            @Override
            public void loadMoreData() {
                mQueryDog.pageNo = mQueryDog.pageNo + 1;
                mPresenter.queryPetsOnSale(mQueryDog, true);
            }
        });

        mSellRecyclerView.setAdapter(mSellAdapt);

        mSellSwipeRefreshLayout = rootView.findViewById(R.id.srv_sell);
        mSellSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mQueryDog.requestId = System.currentTimeMillis();
                mPresenter.queryPetsOnSale(mQueryDog, false);
            }
        });

        mQueryDog = new QueryDog();
        mPresenter.queryPetsOnSale(mQueryDog, false);
    }

    @Override
    public void updatePetsOnSale(SellDogInfo mSellDogInfo, boolean loadMore) {
        if (mSellDogInfo == null || mSellDogInfo.petsOnSale == null) return;
        if (!loadMore) {
            mPetsOnSaleItemList.clear();
            mPetsOnSaleItemSet.clear();
        }
        for (PetsOnSaleItem mPetsOnSaleItem : mSellDogInfo.petsOnSale) {
            if (!mPetsOnSaleItemSet.contains(mPetsOnSaleItem)) {
                mPetsOnSaleItemSet.add(mPetsOnSaleItem);
                mPetsOnSaleItemList.add(mPetsOnSaleItem);
            }
        }

        mSellAdapt.notifyDataSetChanged();
        mSellSwipeRefreshLayout.setRefreshing(false);
        mSellAdapt.setLoaded();
    }
}
